package top.yinjinbiao.mqtt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;
import top.yinjinbiao.mqtt.client.CustomMqttClient;
import top.yinjinbiao.mqtt.consts.WebSocketConstant;
import top.yinjinbiao.mqtt.enums.QosEnums;
import top.yinjinbiao.mqtt.properties.MqttProperties;
import top.yinjinbiao.mqtt.service.MqttService;

import javax.annotation.PostConstruct;
import java.util.HashSet;

/**
 * @Description
 * @Author yin.jinbiao
 * @Date 2021/11/24 9:49
 * @Version 1.0
 */
@Service
public class MqttServiceImpl implements MqttService {

    @Autowired
    private CustomMqttClient customMqttClient;

    @Autowired
    private MqttProperties properties;


    @PostConstruct
    public void init(){
        // 连接服务端
        customMqttClient.connect(properties.getUsername(), properties.getPassword());
        // 订阅一个主题
        customMqttClient.subscribe("/edge/pkey/sn/#", QosEnums.QoS0);
        // 添加给水排水监测客户端集合
        WebSocketConstant.websocketClients.put("WATER_LEVEL", new HashSet<WebSocketSession>());
    }

}
